<?php

namespace app\controller\api;

use app\BaseController;
use app\model\StationBasic;
use app\model\StationOil;
use think\Response;

class Station extends BaseController
{
    public function lst(): Response
    {
        return $this->success($this->app->make(StationBasic::class)->userStation());
    }

    public function detail(): Response
    {
        return $this->success($this->app->make(StationBasic::class)->detail());
    }

    public function mathDiscount(): Response
    {
        $param = $this->request->param();
        $oil = $this->app->make(StationOil::class)->with(['station'])->where('id', $param['oil'])->find();
        if (empty($oil)) throw new \Exception("优惠计算失败！");
        $station = $oil['station'];
        $price = ["sat_price", "mon_price", "tue_price", "wed_price", "thu_price", "fri_price", "sat_price"][date("w")];
        $discount = mathDiscount($station['rate_unit'], $station[$price] > 0 ? $station[$price] : $station['default_price'], $oil['gun_price']);
        //计算加油升数
        $litter = mathLitter($param['money'], $station['litter_type'], $oil['gun_price']);
        //计算本次优惠后的金额
        $money = $discount != $oil['gun_price'] ? round($litter * $discount, 2) : $param['money'];
        //计算优惠折扣
        $discount = bcsub($param['money'], $money, 2);
        //判断是否有服务费
        $ser_price = 0;
        if ($station['ser_price'] > 0) {
            $ser_price = round($param['money'] * bcdiv($station['ser_price'], 100, 4), 2);
        }
        $total = bcadd($money, $ser_price, 2);
        return $this->success(compact('litter', 'discount', 'money', 'ser_price', 'total'));
    }
}
